package com.bana.fvs.app.main.adapter;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bana.fvs.R;
import com.bana.fvs.app.listener.AdapterListener;
import com.bana.fvs.app.module.detaile.entity.FoglightDetailInfo;

import java.util.List;

public class FogLampAdapter extends RecyclerView.Adapter<FogLampAdapter.ViewHolder> {
    public List<FoglightDetailInfo> datas = null;

    public FogLampAdapter(List<FoglightDetailInfo> datas) {
        this.datas = datas;
    }

    //创建新View，被LayoutManager所调用
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_layout_single_error_view, viewGroup, false);
        return new ViewHolder(view);
    }

    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        FoglightDetailInfo bean = datas.get(position);
        if (null == bean) return;
        if (TextUtils.equals("1", bean.getSbzt())) {
            holder.errorEquipState.setImageResource(R.drawable.equipment_normal);
        } else {
            holder.errorEquipState.setImageResource(R.drawable.equipment_error);
        }
        holder.tv_fog_elc_status.setVisibility(View.VISIBLE);//控制【电压状态】view显示出来，因为Item的layout是复用的，，，，
        holder.errorTvName.setText(String.format("%d", bean.getWdbm()));//雾灯编码
        holder.tv_fog_name.setText(String.format("%s", bean.getWdztsm()));//温度状态-2018年04月17日21:21:40修改
        holder.tv_fog_elc_status.setText(String.format("%s", bean.getDyztsm()));//电压状态-2018年04月17日21:21:40修改


        holder.itemBaseContainer.setOnClickListener(v -> {
            if (null != mOnItemClickListener) {
                mOnItemClickListener.onClickListener(position, datas.get(position));
            }
        });
        //
        ImageView imageView = holder.errorRightHandleIcon;
        imageView.setOnClickListener(v -> {
            if (null != mListener) {
                mListener.onOnHandleBtnClick(position, imageView);
            }
        });

    }

    //获取数据的数量
    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {

        //最外层容器
        LinearLayout itemBaseContainer;
        //Error页面
        private RelativeLayout itemSingleErrorContainer;
        private TextView errorTvName;
        private TextView tv_fog_name;
        private TextView tv_fog_elc_status;
        private ImageView errorEquipState;
        private ImageView errorRightHandleIcon;



        public ViewHolder(View itemView) {
            super(itemView);
            itemBaseContainer = itemView.findViewById(R.id.itemBaseContainer);

            /**-----------------------------Error------------------------------------*/
            itemSingleErrorContainer = itemView.findViewById(R.id.itemSingleErrorContainer);
            errorTvName = itemView.findViewById(R.id.errorTvName);
            tv_fog_name = itemView.findViewById(R.id.tv_fog_name);
            tv_fog_elc_status = itemView.findViewById(R.id.tv_fog_elc_status);
            errorEquipState = itemView.findViewById(R.id.errorEquipState);
            errorRightHandleIcon = itemView.findViewById(R.id.errorRightHandleIcon);
            /**---------------------------------------------------------------------*/

        }
    }

    /**
     * 回调
     */
    private AdapterListener.OnItemClickListener<FoglightDetailInfo> mOnItemClickListener;


    public void setOnItemClickListener(AdapterListener.OnItemClickListener<FoglightDetailInfo> onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    /**
     * 右边图片点击回调
     */
    private AdapterListener.OnRightHandleIconClickListener mListener;

    public void setOnHandleBtnClickListener(AdapterListener.OnRightHandleIconClickListener l) {
        this.mListener = l;
    }
}
